package com.example.cardgameapp

import android.os.Bundle
import android.content.Intent
import android.widget.Button
import android.widget.TextView
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import android.graphics.BitmapFactory

class MainActivity : AppCompatActivity() {
    private companion object {
        const val REQUEST_EDIT_INFO = 1001
        const val REQUEST_CARD_GAME = 1002
    }

    private lateinit var tvNameLabel: TextView
    private lateinit var tvGenderLabel: TextView
    private lateinit var tvStudentIdLabel: TextView
    private lateinit var tvPhoneLabel: TextView
    private lateinit var tvEmailLabel: TextView
    private lateinit var tvHobbyLabel: TextView
    private lateinit var tvHighestScoreLabel: TextView
    private lateinit var ivAvatar: ImageView // 头像ImageView
    private var highestScore = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initViews()
        initDefaultInfo()
        setupButtonListeners()
    }

    private fun initViews() {
        ivAvatar = findViewById(R.id.iv_avatar) // 绑定头像控件
        tvNameLabel = findViewById(R.id.tv_name_label)
        tvGenderLabel = findViewById(R.id.tv_gender_label)
        tvStudentIdLabel = findViewById(R.id.tv_student_id_label)
        tvPhoneLabel = findViewById(R.id.tv_phone_label)
        tvEmailLabel = findViewById(R.id.tv_email_label)
        tvHobbyLabel = findViewById(R.id.tv_hobby_label)
        tvHighestScoreLabel = findViewById(R.id.tv_highest_score_label)
    }

    private fun initDefaultInfo() {
        tvNameLabel.text = getString(R.string.label_name)
        tvGenderLabel.text = getString(R.string.label_gender)
        tvStudentIdLabel.text = getString(R.string.label_student_id)
        tvPhoneLabel.text = getString(R.string.label_phone)
        tvEmailLabel.text = getString(R.string.label_email)
        tvHobbyLabel.text = getString(R.string.label_hobby)
        tvHighestScoreLabel.text = getString(R.string.label_highest_score) + "0"
    }

    private fun setupButtonListeners() {
        findViewById<Button>(R.id.btn_edit_info).setOnClickListener {
            val intent = Intent(this, EditInfoActivity::class.java)
            startActivityForResult(intent, REQUEST_EDIT_INFO)
        }

        findViewById<Button>(R.id.btn_card_game).setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java)
            startActivityForResult(intent, REQUEST_CARD_GAME)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_EDIT_INFO && resultCode == RESULT_OK && data != null) {
            // 更新文本信息
            tvNameLabel.text = getString(R.string.label_name) + data.getStringExtra("name").orEmpty()
            tvGenderLabel.text = getString(R.string.label_gender) + data.getStringExtra("gender").orEmpty()
            tvStudentIdLabel.text = getString(R.string.label_student_id) + data.getStringExtra("studentId").orEmpty()
            tvPhoneLabel.text = getString(R.string.label_phone) + data.getStringExtra("phone").orEmpty()
            tvEmailLabel.text = getString(R.string.label_email) + data.getStringExtra("email").orEmpty()
            tvHobbyLabel.text = getString(R.string.label_hobby) + data.getStringExtra("hobbies").orEmpty()

            // 更新头像
            val avatarPath = data.getStringExtra("avatarPath")
            avatarPath?.let {
                val bitmap = BitmapFactory.decodeFile(it)
                ivAvatar.setImageBitmap(bitmap)
            }
        } else if (requestCode == REQUEST_CARD_GAME && resultCode == RESULT_OK && data != null) {
            val newScore = data.getIntExtra("highestScore", 0)
            if (newScore > highestScore) {
                highestScore = newScore
                tvHighestScoreLabel.text = getString(R.string.label_highest_score) + highestScore
            }
        }
    }
}